//
//  BdjCell.m
//  Together
//
//  Created by Wang Shuguang on 12-12-17.
//  Copyright (c) 2012年 Dawn. All rights reserved.
//

#import "BdjCell.h"
#import <QuartzCore/QuartzCore.h>
#import "SDImageCache.h"

@implementation BdjCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        
        // 来源
        nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH/2 - 10, 20)];
        nameLbl.font = [UIFont systemFontOfSize:14.0f];
        nameLbl.textColor = [UIColor colorWithRed:57.0/255.0f green:70.0f/255.0f blue:83.0/255.0f alpha:1.0];
        [self addSubview:nameLbl];
        [nameLbl setBackgroundColor:[UIColor clearColor]];
        [nameLbl release];
        
        // 发布日期
        timeLbl = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2 - 10, 20)];
        timeLbl.font = [UIFont systemFontOfSize:12.0f];
        timeLbl.textColor = [UIColor colorWithRed:244.0/255.0f green:137.0f/255.0f blue:13.0/255.0f alpha:1.0];
        timeLbl.textAlignment = UITextAlignmentRight;
        [timeLbl setBackgroundColor:[UIColor clearColor]];
        [self addSubview:timeLbl];
        [timeLbl release];
    
        
        // 内容
        textLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, SCREEN_WIDTH - 20, 20)];
        textLbl.font = [UIFont systemFontOfSize:14.0f];
        textLbl.textColor = [UIColor colorWithRed:66.0/255.0f green:66.0/255.0f blue:66.0/255.0f alpha:1.0];
        textLbl.lineBreakMode = UILineBreakModeWordWrap;
        textLbl.numberOfLines = 0;
        [textLbl setBackgroundColor:[UIColor clearColor]];
        [self addSubview:textLbl];
        [textLbl release];
        
        // 图片
        gifImageView = [[SCGIFImageView alloc] init];
        gifImageView.contentMode = UIViewContentModeScaleAspectFit;
        gifImageView.frame = CGRectMake(10, textLbl.frame.origin.y + textLbl.frame.size.height, SCREEN_WIDTH - 20, 0);
        [self addSubview:gifImageView];
        [gifImageView release];
        
        CALayer *layer = gifImageView.layer;
        [layer setBorderWidth:1];
        [layer setBorderColor:[[UIColor lightGrayColor] CGColor]];
    }
    return self;
}

- (void) setName:(NSString *)name{
    [nameLbl setText:name];
}

- (void) setTime:(NSString *)time{
    [timeLbl setText:time];
}

- (void) setContent:(NSString *)text{
    CGSize size = CGSizeMake(textLbl.frame.size.width, 10000);
    size = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
    textLbl.frame = CGRectMake(textLbl.frame.origin.x, textLbl.frame.origin.y, textLbl.frame.size.width, size.height + 10);
    
    [textLbl setText:text];
    
    gifImageView.frame = CGRectMake(gifImageView.frame.origin.x, textLbl.frame.origin.y + textLbl.frame.size.height, gifImageView.frame.size.width, gifImageView.frame.size.height);
}

- (void) setImageWithURL:(NSURL *)url imageSize:(CGSize)imageSize isGif:(BOOL)isGif{
    if (imageSize.height <= 0) {
        gifImageView.hidden = YES;
    }else{
        gifImageView.hidden = NO;
        float width =  MIN(imageSize.width, SCREEN_WIDTH - 20);
        float height =  MIN(imageSize.height, width * imageSize.height / imageSize.width);
        CGSize realImageSize = CGSizeMake(width, height);
        gifImageView.frame = CGRectMake(gifImageView.frame.origin.x, gifImageView.frame.origin.y, SCREEN_WIDTH - 20, realImageSize.height);
        SDImageCache *imageCache = [SDImageCache sharedImageCache];
        gifImageView.isGif = isGif;
        gifImageView.gifPath = [imageCache cachePathForKey:[url absoluteString]];
        [gifImageView setImageWithURL:url placeholderImage:nil options:SDWebImageLowPriority];
//        NSString *imagePath = [imageCache cachePathForKey:[url absoluteString]];
//        NSFileManager *fileManager = [NSFileManager defaultManager];
//        
//        if ([fileManager fileExistsAtPath:imagePath]) {
//            NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
//            if (isGif) {
//                
//                [gifImageView loadData:imageData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
//            }else{
//                [gifImageView loadData:imageData MIMEType:@"image/jpg" textEncodingName:nil baseURL:nil];
//            }
//        }else{
//            [gifImageView loadRequest:[NSURLRequest requestWithURL:url]];
//        }
       
    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
